% returns the weighted sum of spectra
% latest date 03.04.2003
% to be used with robspm.m
%changed as 0 weights may relut in destabilization
function[SPM,SPMat] = WeightSPM(SPMat,ProcDef,i),

A = size(SPMat);

chk = ProcDef.Weights(i,:);
if any(chk) == 0,
    disp('No Weighting done...');% in case all the weights are zero
    ProcDef.Weights(i,:) = ones([1,length(ProcDef.Weights(i,:))]);
end;
if nargin == 3, % Weighted average
	
    for j = 1:A(1),
   	SPMat(j,:,:) = SPMat(j,:,:)*ProcDef.Weights(i,j);
	end;

	factor = sum(ProcDef.Weights(i,:));

	SPM = sum(SPMat)/factor;
elseif nargin == 1,
   SPM = sum(SPMat)/A(1); % Just average
end;

